package com.yonglian.system.service.impl;

import java.io.IOException;
import java.util.List;

import com.yonglian.common.config.YonglianCMSConfig;
import com.yonglian.common.exception.file.InvalidExtensionException;
import com.yonglian.common.utils.DateUtils;
import com.yonglian.common.utils.ShiroUtils;
import com.yonglian.common.utils.file.FileUploadUtils;
import com.yonglian.common.utils.file.MimeTypeUtils;
import com.yonglian.system.mapper.CmsActivityMapper;
import com.yonglian.system.service.ICmsActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yonglian.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yonglian.system.domain.CmsActivity;
import com.yonglian.system.mapper.CmsProjectBaseMapper;
import com.yonglian.system.domain.CmsProjectBase;
import com.yonglian.system.service.ICmsProjectBaseService;
import com.yonglian.common.core.text.Convert;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目基础信息Service业务层处理
 *
 * @author gyok
 * @date 2024-05-28
 */
@Service
public class CmsProjectBaseServiceImpl implements ICmsProjectBaseService
{
    @Autowired
    private CmsProjectBaseMapper cmsProjectBaseMapper;

    @Autowired
    private CmsActivityMapper cmsActivityMapper;

    @Autowired
    private ICmsActivityService cmsActivityService;

    /**
     * 查询项目基础信息
     *
     * @param id 项目基础信息主键
     * @return 项目基础信息
     */
    @Override
    public CmsProjectBase selectCmsProjectBaseById(Long id)
    {
        return cmsProjectBaseMapper.selectCmsProjectBaseById(id);
    }

    /**
     * 查询项目基础信息列表
     *
     * @param cmsProjectBase 项目基础信息
     * @return 项目基础信息
     */
    @Override
    public List<CmsProjectBase> selectCmsProjectBaseList(CmsProjectBase cmsProjectBase)
    {
        return cmsProjectBaseMapper.selectCmsProjectBaseList(cmsProjectBase);
    }

    /**
     * 新增项目基础信息
     *
     * @param cmsProjectBase 项目基础信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCmsProjectBase(CmsProjectBase cmsProjectBase)
    {
        cmsProjectBase.setCreateTime(DateUtils.getNowDate());
        int rows = cmsProjectBaseMapper.insertCmsProjectBase(cmsProjectBase);
        insertCmsActivity(cmsProjectBase);
        return rows;
    }

    /**
     * 修改项目基础信息
     *
     * @param cmsProjectBase 项目基础信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCmsProjectBase(CmsProjectBase cmsProjectBase)
    {
        Long[] activityIdList = cmsProjectBase.getCmsActivityList() != null ?
                cmsProjectBase.getCmsActivityList().stream().filter(a -> a.getId() != null).map(a -> a.getId()).toArray(Long[]::new)
                : new Long[0];
        cmsActivityMapper.deleteCmsActivityByNotInList(cmsProjectBase.getId(), activityIdList);
        insertCmsActivity(cmsProjectBase);
        return cmsProjectBaseMapper.updateCmsProjectBase(cmsProjectBase);
    }

    /**
     * 批量删除项目基础信息
     *
     * @param ids 需要删除的项目基础信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCmsProjectBaseByIds(String ids)
    {
        cmsActivityMapper.deleteCmsActivityByProjectIds(Convert.toStrArray(ids));
        return cmsProjectBaseMapper.deleteCmsProjectBaseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目基础信息信息
     *
     * @param id 项目基础信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCmsProjectBaseById(Long id)
    {
        cmsActivityMapper.deleteCmsActivityByProjectId(id);
        return cmsProjectBaseMapper.deleteCmsProjectBaseById(id);
    }

    /**
     * 新增活动信息
     *
     * @param cmsProjectBase 项目基础信息对象
     */
    public void insertCmsActivity(CmsProjectBase cmsProjectBase)
    {
        List<CmsActivity> cmsActivityList = cmsProjectBase.getCmsActivityList();
        if (StringUtils.isNull(cmsActivityList)) {
            return;
        }
        Long id = cmsProjectBase.getId();
        List<CmsActivity> insertList = cmsProjectBase.getCmsActivityList().stream()
                .filter(a -> a.getId() == null)
                .map(a -> {
                    cmsActivityService.processImages(a);
                    a.setProjectId(id);
                    a.setCreateTime(DateUtils.getNowDate());
                    a.setCreateBy(ShiroUtils.getSysUser().getUserName());
                    return a;
                }).toList();
        if (!insertList.isEmpty()) {
            cmsActivityMapper.batchInsertCmsActivity(insertList);
        }
        List<CmsActivity> updateList = cmsProjectBase.getCmsActivityList().stream()
                .filter(a -> a.getId() != null)
                .map(a -> {
                    cmsActivityService.processImages(a);
                    a.setProjectId(id);
                    return a;
                })
                .toList();
        if (!updateList.isEmpty()) {
            cmsActivityMapper.batchUpdateCmsActivity(updateList);
        }
    }

}
